package com.css.sword.knowing.toolkit.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.util.Properties;

/**
 * @author 作者 E-mail:caihx@css.com.cn
 * @version 创建时间：2017年3月10日 上午10:43:28
 * 
 */
public class CssPropertiesTool {
	private static boolean loadSuccessfulInfo(String fname) {
		System.out.println("Load " + fname + " successful");
		return true;
	}

	private static void printFailedInfo(String fileName) {
		StringBuilder sbInfo = new StringBuilder("请将" + fileName + "放在下列目录：\n"); // 打印一些友好的tips
		String classPath = (String) System.getProperties().get("java.class.path");
		if (classPath != null) {
			for (String path : classPath.split(File.pathSeparator)) {
				if (new File(path).isDirectory()) {
					sbInfo.append(path).append('\n');
				}
			}
		}
		String userPath = (String) System.getProperties().get("user.dir");
		if (userPath != null) {
			for (String path : userPath.split(File.pathSeparator)) {
				if (new File(path).isDirectory()) {
					sbInfo.append(path).append('\n');
				}
			}
		}

		sbInfo.append("Web项目则请放到下列目录：\n" + "Webapp/WEB-INF/lib\n" + "Webapp/WEB-INF/classes\n" + "Appserver/lib\n"
				+ "JRE/lib\n");
		System.out.println(sbInfo);
	}

	/**
	 * 尝试从不同位置读取属性文件，读取成功后返回，优先级依次为：为打包的class目录、user目录、jar包所在目录、jar包内
	 * 
	 * @param classLoader
	 * @param fname
	 *            属性文件名称
	 * @return
	 */
	public static Properties load(ClassLoader classLoader, String fname) {
		Properties p = new Properties();
		String file = "";

		// 读取未打包配置文件
		try {
			file = classLoader.getResource(fname).getFile();
			if (new File(file).exists()) {
				FileInputStream in = new FileInputStream(file);
				p.load(new InputStreamReader(in, "UTF-8"));
				return p;
			}
		} catch (IOException e) {
		} catch (Exception e) {
		}

		// 读取user目录配置文件
		try {
			file = Paths.get(System.getProperty("user.dir"), fname).toString();
			if (new java.io.File(file).exists()) {
				FileInputStream in = new FileInputStream(file);
				p.load(new InputStreamReader(in, "UTF-8"));
				return p;
			}
		} catch (IOException e) {
		} catch (Exception e) {
		}

		// 读取jar包所在目录配置文件
		try {
			file = Paths.get(CssJarToolUtil.getJarDir(), fname).toString();
			if (new java.io.File(file).exists()) {
				FileInputStream in = new FileInputStream(file);
				p.load(new InputStreamReader(in, "UTF-8"));
				return p;
			}
		} catch (IOException e) {
		} catch (Exception e) {
		}

		// 读取包内配置文件
		try {
			p.load(new InputStreamReader(classLoader.getResourceAsStream(fname), "UTF-8"));
			return p;
		} catch (IOException e) {
		} catch (Exception e) {
		}

		printFailedInfo(fname);
		return null;
	}

	/**
	 * 取属性文件所在的位置
	 * 
	 * @param classLoader
	 * @param fname
	 *            属性文件名称
	 * @return 属性文件所在路径
	 */
	public static String getPath(ClassLoader classLoader, String fname) {
		String file = null;

		// 读取未打包配置文件
		try {
			file = classLoader.getResource(fname).getFile();
			if (new File(file).exists()) {
				return file;
			}
		} catch (Exception e) {
		}

		// 读取user目录配置文件
		try {
			file = Paths.get(System.getProperty("user.dir"), fname).toString();
			if (new java.io.File(file).exists()) {
				return file;
			}
		} catch (Exception e) {
		}

		// 读取jar包所在目录配置文件
		try {
			file = Paths.get(CssJarToolUtil.getJarDir(), fname).toString();
			if (new java.io.File(file).exists()) {
				return file;
			}
		} catch (Exception e) {
		}

		return null;
	}
}
